<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Menu;
class menuController extends Controller
{   
    /**
     * 删除已选
     */
    public function deleteChecked(Request $request){
        if(!$request->has('ids')){
            return json(101,'无已选菜单');
        }
        $ids=explode(',',$request->input('ids'));
        $menu=Menu::whereIn('id',$ids)->delete();
        return json(200,'删除已选成功');
    }
    /**
     * 取消资源路由
     */
    public function cancelResource(Request $request){
        $id=$request->input('id');
        if(!$id){
            return json(101,'缺少参数');
        }
        $res=Menu::where(['pid'=>$id,'is_resource'=>1])->delete();
        if($res)  return json(200,'取消成功');
        return json(105,'取消失败');
    }
    /**
     * 创建资源路由
     */
    public function createResource(Request $request){
        $id=$request->input('id');
        if(!$id){
            return json(101,'缺少参数');
        }
        $menu=Menu::select('menu_name','controller_name')->find($id);
        if(!$menu){
            return json(101,'菜单不存在');
        }

        $resources=[
            ['en'=>'创建页面','method_name'=>'create'],
            ['en'=>'创建操作','method_name'=>'store'],
            ['en'=>'编辑页面','method_name'=>'edit'],
            ['en'=>'编辑操作','method_name'=>'update'],
            ['en'=>'删除操作','method_name'=>'destroy'],
        ];
        for ($i=0; $i < 5; $i++) { 
            $createData=[
                'menu_name'=>$menu->menu_name.$resources[$i]['en'],
                'controller_name'=>$menu->controller_name,
                'method_name'=>$resources[$i]['method_name'],
                'pid'=>$id,
                'is_resource'=>1
            ];
            $where=[
                'controller_name'=>$menu->controller_name,
                'method_name'=>$resources[$i]['method_name'],
            ];
            if(!Menu::where($where)->exists()){
                Menu::create($createData);
            }
        }
        return json(200,$menu->menu_name.'的资源路由创建成功');
    }
   
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //遍历未曾注册到菜单的控制器方法
        $path=['','Http','Controllers','admin',''];
     
        $invalid=[];
        $dir=app_path().join(DIRECTORY_SEPARATOR,$path);
        
        $files=scandir($dir);
        foreach ($files as $key => $value) {
           if(strrchr($value,'.')=='.php'){
                $controllerName=explode('.',$value)[0];
                $file=fopen($dir.$value,'r');
                $content=fread($file,filesize($dir.$value));
             
                 //判断trait
                 if(strpos($content,'trashedMethods')&&$controllerName!='menuController'){
                    $traitMethods=['deleteChecked','restoreChecked','forceDeleteChecked'];
                    foreach ($traitMethods as $method) {
                        $where=[
                            'controller_name'=>$controllerName,
                            'method_name'=>$method
                        ];
                        $is_has=Menu::where($where)->exists();
                        if(!$is_has){
                            $invalid[]=[
                                'controller_name'=>$controllerName,
                                'method_name'=>$method
                            ];
                        }                        
                    }
                }

                preg_match_all("/public function[\s]([\W\w]*)\(/U",$content,$reg);
                $methods=$reg[1];
                foreach ($methods as $kk => $methodName) {
                    $where=[
                        'controller_name'=>$controllerName,
                        'method_name'=>$methodName
                    ];
                    $is_has=Menu::where($where)->exists();
                    if(!$is_has){
                        $invalid[]=$where;
                    }
                }

               
                fclose($file);
           }
        }

        $pageListData=Menu::getTree(0,1);
        // dd($pageListData);
        return view('admin.menu.index',compact('pageListData','invalid'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $selectData=Menu::getTree(0,1);
        return view('admin.menu.form',compact('selectData'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $schema=[
            'menu_name'=>'required',
            'controller_name'=>'required',
            'method_name'=>'required',
            'pid'=>'required',
        ];
        $request->validate($schema);
        $createData=$request->only(['menu_name','controller_name','method_name','route_path','pid','sort','is_nav','icon']);
        Menu::create($createData);
        return json(200,'创建成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $selectData=Menu::getTree(0,1);
        $editData=Menu::find($id);
        return view('admin.menu.form',compact('selectData','editData'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $schema=[
            'menu_name'=>'required',
            'controller_name'=>'required',
            'method_name'=>'required',
            'pid'=>'required',
        ];
        $request->validate($schema); 
        $updateData=$request->only(['menu_name','controller_name','method_name','route_path','pid','sort','is_nav','icon']);
        Menu::find($id)->update($updateData);
        return json(200,'修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $is_hasChild=Menu::where('pid',$id)->exists();
        if($is_hasChild){
            return json(105,'删除失败,菜单下含有子菜单');
        }
        $res=Menu::find($id)->delete();
        if($res)  return json(200,'删除成功');
        return json(105,'删除失败');
    }
}
